<?
$pagename=LA_ADD_AD;
include_once("header_inc.php");
$tplItem=new TplLoad;
check_valid_user("");
include_once("member_header.php");	

$valid_user = $_SESSION["valid_user"];
$mainType=getParam("mainType",ID_CAT_COMPRA);

$resultat = mysql_query ("select count(*),sum(ad_views),sum(ad_votes),count(ad_voters),(select count(ad_id) from $ads_tbl where ad_owner = $valid_user AND ad_is_sold=1) as count_ad_id
							 from $ads_tbl where ad_owner = $valid_user");
$rad = mysql_fetch_array($resultat);
$ad_views_total = $rad["sum(ad_views)"];
$ads_total = $rad["count(*)"];

$resultat = mysql_query ("select sum(user_contacted) from $usr_tbl where user_id = $valid_user");
$rusr = mysql_fetch_array($resultat);
$num_contacted = $rusr["sum(user_contacted)"];
if (!$num_contacted)
	$num_contacted = 0;

$number = $ads_total;
if (!$number)
	$number = 0;


$number_sold = $rad["count_ad_id"];

$number_hits = $ad_views_total;
if (!$number_hits)
	$number_hits = 0;

$number_ad_votes = $rad["sum(ad_votes)"];
if (!$number_ad_votes)
	$number_ad_votes = 0;

$number_ad_voters = $rad["count(ad_voters)"];
if (!$number_ad_voters)
	$number_ad_voters = 0;

	
$score=0;
if ($number_ad_voters>0)
	$score = $number_ad_votes/$number_ad_voters;

$sql = "select user_votes,user_voters from $usr_tbl where user_id = $valid_user";
$res = mysql_query($sql);
$row = mysql_fetch_array($res);

$votes = $row["user_votes"];
$voters = $row["user_voters"];
$cur_grade=0;

if ($voters > 0)
{
	$cur_grade = round($votes / $voters, 1);
}
$la_from_here3 = ereg_replace("\{EMAIL\}",$from_adress, LA_FROM_HERE3);
$from= formatString($la_from_here3,array("$from_adress"));

$tplMember=new TplLoad;
if ($set_payments)
{
	$tplMember->assign("payments",1);	
	$sql="select a.mainType, pay_membership_expire,pay_upload,pay_upload_video,pay_vendor,pay_credits,pay_bold,pay_premium 
	, coalesce(b.pname,'EF_GIF') as name from $user_credits a left join $pkg_tbl b on a.package_id=b.pid where user_id=" . $valid_user." order by mainType";
	
	$r=q($sql);
	$NotEmpty=false;
	$todosLosCreditos= array();
	$MainTypeAnterior = -100;
	while($ro = mysql_fetch_array($r)){
		$NotEmpty=true;
		$lExpireDate=formatDateShort($ro["pay_membership_expire"]);
		
		if (getExpireStatus($valid_user))
			$tplMember->assign("expire_date",$lExpireDate);
		$creditos= array();
		$creditos["pay_upload"] =$ro["pay_upload"];
		$creditos["pay_credits"]=$ro["pay_credits"];
		$creditos["pay_bold"]   =$ro["pay_bold"];
		$creditos["pay_premium"]=$ro["pay_premium"];
		if($ro["name"] == 'EF_GIF')	
			$creditos["package_name"]=EF_GIF;
		else
			$creditos["package_name"]=$ro["name"];
 		if($MainTypeAnterior != $ro["mainType"]) {
			switch( $ro["mainType"] )
	        {
	            case ID_CAT_COMPRA: $creditos["mainTypeName"]=EF_MAINTYPE_CREDIT_VENTA; break;
	            case ID_CAT_ALQUILER: $creditos["mainTypeName"]=EF_MAINTYPE_CREDIT_ALQUILER; break;
	            case ID_CAT_SERVICIOS: $creditos["mainTypeName"]=EF_MAINTYPE_CREDIT_SERVICIOS; break;
	            case -1  : $creditos["mainTypeName"]=EF_MAINTYPE_CREDIT_VENTA_DES; break;
	            default: break;
	        }
 		}else
 		   	$creditos["mainTypeName"]='';
 		$MainTypeAnterior = $ro["mainType"];
        $todosLosCreditos[]=$creditos; //$ro["mainType"]
		/*$tplMember->assign("pay_upload",$ro["pay_upload"]);
		$tplMember->assign("pay_credits",$ro["pay_credits"]);
		$tplMember->assign("pay_bold",$ro["pay_bold"]);
		$tplMember->assign("pay_premium",$ro["pay_premium"]);*/
	}
	$tplMember->assign("isCredits",$NotEmpty);
	$tplMember->assign("todosLosCreditos",$todosLosCreditos);
}

$tplMember->assign("email",$from);
$tplMember->assign("name",$name);
$tplMember->assign("number",$number);
$tplMember->assign("num_contacted","$num_contacted");
$tplMember->assign("number_sold",$number_sold);
$tplMember->assign("number_hits",$number_hits);
$tplMember->assign("cur_grade",round($cur_grade,2));
$tplMember->assign("score",round($score,2));
/*$tplMember->assign("credits",$credits);*/
$tplMember->assign("credits_option",$credits_option);

$tplMember->display("member.tpl");

if ($auto)
{
	// Find, warn and delete expiring ads
	//updateExpireAds("");	
}

include_once("member_footer.php");
include_once("footer_inc.php");
?>